package com.sanpu.kxe.business.trade.view;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.trade.adapter.TransactionAdapter;
import com.sanpu.kxe.business.trade.bean.TransactionOutput;
import com.sanpu.kxe.business.trade.contract.WithdrawListContract;
import com.sanpu.kxe.business.trade.presenter.WithdrawListPresenter;
import com.sanpu.kxe.business.trade.provider.TradeOpenProvider;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.utils.DateCheckPopListener;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.module.wheel.interfaces.IWheelDataChangeCallback;
import com.sanpu.kxe.module.widget.VerticalSwipeRefreshLayout;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Description：提现详情
 * Created by 薛勇军 on 2019/9/25.
 */

public class WithdrawListActivity extends BaseTitleActivity implements WithdrawListContract.View {

    @Bind(R.id.recyclerview)
    RecyclerView vRecyclerview;
    @Bind(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;

    @Bind(R.id.chooseTimeView)
    View chooseTimeView;
    @Bind(R.id.emptyView)
    View emptyView;

    private LinearLayout chooseTimeRll;

    private RoundTextView startDateRtv, endDateRtv;

    private TransactionAdapter mAdapter;

    private WithdrawListContract.Presenter mPresenter;

    private Animation loadInAnimation, loadOutAnimation;

    private DateCheckPopListener vCheckPopListener;

    private int nowPage = 0;

    private String startTime, endTime;

    private boolean isSelectTime = false;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_withdraw_list;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("提现详情");
        commonTitle.showRightImageView(R.drawable.ic_calendar_gray);

        chooseTimeRll = ButterKnife.findById(chooseTimeView, R.id.chooseTimeRll);
        startDateRtv = ButterKnife.findById(chooseTimeView, R.id.startDateRtv);
        endDateRtv = ButterKnife.findById(chooseTimeView, R.id.endDateRtv);

        vRecyclerview.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mAdapter = new TransactionAdapter(mContext);
        vRecyclerview.setAdapter(mAdapter);
        mAdapter.disableLoadMoreIfNotFullPage(vRecyclerview);
        mAdapter.setPreLoadNumber(2);

        final StickyRecyclerHeadersDecoration headersDecor = new StickyRecyclerHeadersDecoration(mAdapter);
        vRecyclerview.addItemDecoration(headersDecor);
        mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                headersDecor.invalidateHeaders();
            }
        });

        mPresenter = new WithdrawListPresenter(this);
        mPresenter.withDrawBillList(nowPage, startTime, endTime);
    }

    @Override
    protected void setListener() {
        super.setListener();
        commonTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isRespondEvent())
                    showChooseTimePop(isSelectTime);
            }
        });
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                nowPage = 0;
                mPresenter.withDrawBillList(nowPage, startTime, endTime);
                mAdapter.setEnableLoadMore(true);
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.withDrawBillList(nowPage, startTime, endTime);
            }
        }, vRecyclerview);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                TransactionOutput transactionOutput = ((TransactionAdapter)adapter).getItem(position);
                if (transactionOutput != null) {
                    TradeOpenProvider.getInstance().openTransactionDetailsActivity(mContext, transactionOutput);
                }
            }
        });
        emptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nowPage = 0;
                mPresenter.withDrawBillList(nowPage, startTime, endTime);
                mAdapter.setEnableLoadMore(true);
            }
        });
        ButterKnife.findById(chooseTimeView, R.id.emptyView).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (isRespondEvent())
                    showChooseTimePop(true);
                return true;
            }
        });
        ButterKnife.findById(chooseTimeView, R.id.closeIv).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (isRespondEvent())
                    showChooseTimePop(true);
                return true;
            }
        });
        ButterKnife.findById(chooseTimeView, R.id.comfirmRtv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nowPage = 0;
                mPresenter.withDrawBillList(nowPage, startTime, endTime);
                mAdapter.setEnableLoadMore(true);
                showChooseTimePop(true);
            }
        });
        chooseTimeView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        startDateRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (vCheckPopListener == null) {
                    vCheckPopListener = new DateCheckPopListener(WithdrawListActivity.this);
                }
                vCheckPopListener.handleRegionCheck(v, startDateRtv, true, new IWheelDataChangeCallback() {
                    @Override
                    public void wheelDataChangeCallback(String wheelData) {
                        if (!TextUtils.isEmpty(wheelData)) {
                            startDateRtv.setText(wheelData);
                            startTime = wheelData;
                        }
                    }
                });
            }
        });
        endDateRtv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (vCheckPopListener == null) {
                    vCheckPopListener = new DateCheckPopListener(WithdrawListActivity.this);
                }
                vCheckPopListener.handleRegionCheck(v, endDateRtv, true, new IWheelDataChangeCallback() {
                    @Override
                    public void wheelDataChangeCallback(String wheelData) {
                        if (!TextUtils.isEmpty(wheelData)) {
                            endDateRtv.setText(wheelData);
                            endTime = wheelData;
                        }
                    }
                });
            }
        });
    }

    /**
     * 显示全部分类pop
     */
    private void showChooseTimePop(boolean selectAll) {
        if (selectAll) {
            isSelectTime = false;
            loadOutAnimation = AnimationUtils.loadAnimation(mContext,
                    R.anim.push_bottom_out);
            loadOutAnimation.setFillAfter(true);
            chooseTimeRll.startAnimation(loadOutAnimation);
            loadOutAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    chooseTimeView.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
        } else {
            isSelectTime = true;
            chooseTimeView.setVisibility(View.VISIBLE);
            loadInAnimation = AnimationUtils.loadAnimation(mContext,
                    R.anim.push_bottom_in);
            loadInAnimation.setFillAfter(true);
            chooseTimeRll.startAnimation(loadInAnimation);
        }
    }

    @Override
    public void showWithdrawListView(List<TransactionOutput> outputs, boolean isFirst) {
        if (outputs != null && outputs.size()>0) {
            vSwipeRefreshLayout.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
            if (isFirst) {
                mAdapter.setNewData(outputs);
            } else {
                mAdapter.addData(outputs);
            }
            nowPage++;
        } else if (isFirst){
            vSwipeRefreshLayout.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                mAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                mAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                mAdapter.loadMoreFail();
                break;
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(WithdrawListContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
